package com.mmall.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

import javax.annotation.Resource;

@Service("redisPool")
@Slf4j
public class RedisPool {

    @Resource(name = "shardedJedisPool")
    private ShardedJedisPool shardedJedisPool;//受spring管理的

    /**
     * 拿到redis的资源
     * @return
     */
    public ShardedJedis instance(){
        return shardedJedisPool.getResource();//启动拿到资源
    }

    public void safeClose(ShardedJedis shardedJedis){
        try {
            if (shardedJedis != null){
                shardedJedis.close();
            }
        }catch (Exception e){
            log.error("redis资源异常：",e);
        }
    }
}
